<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>添加部门</title>
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<link rel="stylesheet" href="../../css/oksub.css">
	<link rel="stylesheet" href="../../libme/modules/dtree/dtree.css">
	<link rel="stylesheet" href="../../libme/modules/dtree/font/dtreefont.css">
	<script type="text/javascript" src="../../lib/loading/okLoading.js"></script>
</head>
<body>
<div class="ok-body">
	<!--form表单-->
	<!--<form class="layui-form layui-form-pane ok-form">-->
	<form class="layui-form ok-form" lay-filter="post-add">
		<input type="hidden" name="deptId" id="deptId" value="">
		<div class="layui-form-item">
			<label class="layui-form-label">部门名称</label>
			<div class="layui-input-inline">
				<input type="text" name="deptName" placeholder="部门名称" autocomplete="off"
					   class="layui-input" lay-verify="required" lay-verType="tips" id="deptName">
			</div>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label">上级部门</label>
			<div class="layui-input-inline">
				<input type="text" name="pDeptName" placeholder="上级部门" autocomplete="off" class="layui-input"
					   id="pDeptName">
				<input type="hidden" name="parentId" id="parentId" value=""/>
			</div>
			<button type="button" class="layui-btn layui-btn-normal" id="deptBtn">选择部门</button>
		</div>

		<!--<div class="layui-form-item">-->
			<!--<label class="layui-form-label">部门领导</label>-->
			<!--<div class="layui-input-inline">-->
				<!--<ul id="leaders" class="dtree" data-id="0" ></ul>-->
			<!--</div>-->
			<!--<button type="button" class="layui-btn layui-btn-primary" id="lreset">重置</button>-->
		<!--</div>-->

		<div class="layui-form-item">
			<label class="layui-form-label">部门领导</label>
			<div class="layui-input-inline">
				<input type="text" name="leader" placeholder="部门领导" autocomplete="off" class="layui-input" id="leader" lay-verify="required" lay-verType="tips">
			</div>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label">手机号码</label>
			<div class="layui-input-inline">
				<input type="text" name="mobile" placeholder="手机号码" autocomplete="off" class="layui-input" id="mobile" lay-verify="required|mobile" lay-verType="tips">
			</div>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label">电子邮箱</label>
			<div class="layui-input-inline">
				<input type="text" name="email" placeholder="电子邮箱" autocomplete="off" class="layui-input" id="email" lay-verify="email" lay-verType="tips">
			</div>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label">排序号码</label>
			<div class="layui-input-inline">
				<input type="text" name="sortno" placeholder="排序号码" autocomplete="off" class="layui-input" id="sortno">
			</div>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label">状态</label>
			<div class="layui-input-inline">
				<input type="checkbox" name="deptStatus" lay-skin="switch" lay-text="启用|停用">
			</div>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label">备注</label>
			<div class="layui-input-inline">
				<input type="text" name="remark" placeholder="用户什么也没有留下" autocomplete="off" class="layui-input">
			</div>
		</div>

		<div class="layui-form-item">
			<div class="layui-input-block">
				<button class="layui-btn" lay-submit lay-filter="add">立即提交</button>
				<button type="reset" class="layui-btn layui-btn-primary">重置</button>
			</div>
		</div>
	</form>
</div>
<!--js逻辑-->
<script src="../../lib/layui/layui.js"></script>
<script src="../../libme/pages/body.js"></script>
<script src="../../libme/script/pinyin.js"></script>
<script type="text/javascript">
	layui.use(["element", "form", "jquery", "okMock", "api", "dtree"], function () {
		let form = layui.form;
        let dtree = layui.dtree;
        let okMock = layui.okMock;
        let $ = layui.jquery;
        let api = layui.api;
        okLoading.close();

        var SUCCESS = 'SUCCESS';

		// tree.render({
        // 	elem: "#permissionTree",
        // 	//data: okMock.api.permission.list,
        // 	data: data,//从本地 localStorage 中取值
        // 	showCheckbox: true
        // });

		form.verify({
            email: function (value, item) {
                return api.regEmail(value);
            },
            mobile: function (value, item) {
                return api.regMobile(value);
            }
		})
        form.on("submit(add)", function (data) {
			// TODO 权限节点校验
			var doReq = $("#deptId").val().trim() == '' ? okMock.api.dept.create : okMock.api.dept.update;
			data.field.deptStatus == undefined ? data.field.deptStatus = '1' : data.field.deptStatus = '0';
			api.request({
				url: doReq,
				where: data.field,
				dataType: api.jsonType.ujson
			},function (resp, stat) {
                api.greenTickMsg("添加成功", function () {
                    parent.layer.close(parent.layer.getFrameIndex(window.name));
                });
            })
			return false;
		});

        $('#deptName').blur(function () {
			var name = $(this).val().trim();
			var that = this;
			$.ajax({
				url:okMock.api.dept.checkExists,
				data:{deptName: name},
				type:'post',
				dataType:'text',
				success: function (res, status) {
				    if(res === SUCCESS) {
                        api.tips("部门名称已经存在，请更换新的名称。", that, 'd');
                    }
                }
			})
        });

        function showDeptDtree() {
            layer.open({
                type: 1,  //type:0 也行
                title: "选择上级部门",
                area: ["400px", "80%"],
                content: '<ul id="depts" class="dtree" data-id="0"></ul>',
                btn: ['确认'],
                success: function(layero, index){
                    var DTree = dtree.render({
                        //  obj: $(layero).find("#openTree1"),    如果直接用elem加载不出来，则可以使用这个方式加载jquery的DOM
                        elem: "#depts",
                        url: okMock.api.dept.dtree,
                        dataStyle: "layuiStyle",    //使用layui风格的数据格式（使用"defaultStyle"出现无法加载数据情况）
                        dataFormat: "list",         //配置data的风格为list
                        response:{//返回JSON字段与DTree字段不一致时需要使用
                            statusCode: 100, //返回码
                        },//修改response中返回数据的定义
                        done: function(res, $ul, first){
                            if(first) {
                                dtree.dataInit("depts", $('#parentId').val());
                            }
                        }
                    });

                    // 绑定节点的双击
                    dtree.on("nodedblclick('depts')", function(obj){
                        $("#parentId").val(obj.param.nodeId);
                        $("#pDeptName").val(obj.param.context);

                        layer.close(index);
                    });
                },
                yes: function(index, layero) {
                    var param = dtree.getNowParam("depts"); // 获取当前选中节点
                    $("#parentId").val(param.nodeId);
                    $("#pDeptName").val(param.context);
                    layer.close(index);
                }
            });
        }
        $('#deptBtn,#pDeptName').click(function () {
            showDeptDtree();
        });

        $(function () {//页面加载

        })
	})
</script>
</body>
</html>
